/**
 * 
 */
package org.swing.utility.net.exec;

import java.io.IOException;
import java.io.InputStream;

import org.swing.utility.net.core.LessBytes;

/**
 * @author lqnhu
 *
 */
public class SimpleExec {
	private Process process;
	private byte[] stdout;
	private byte[] stderr;
	private int exit;

	public SimpleExec(String cmd) throws IOException {
		process = Runtime.getRuntime().exec(cmd);
	}

	public SimpleExec(String cmd[]) throws IOException {
		process = Runtime.getRuntime().exec(cmd);
	}

	public SimpleExec join() throws InterruptedException, IOException {
		InputStream stdoutIn = process.getInputStream();
		InputStream stderrIn = process.getErrorStream();
		stdout = LessBytes.readFully(stdoutIn);
		stderr = LessBytes.readFully(stderrIn);
		exit = process.waitFor();
		return this;
	}

	public byte[] stdout() {
		return stdout;
	}

	public String stdoutString() {
		return LessBytes.toString(stdout);
	}

	public byte[] stderr() {
		return stderr;
	}

	public String stderrString() {
		return LessBytes.toString(stderr);
	}

	public int exitCode() {
		return exit;
	}
}

